<script>
import QRCode from 'qrcodejs2'

export default {
  // 二维码组件
  name: 'QrCodeView',
  props: {
    // 内容
    qrCodeText: {
      type: String,
      required: true,
    },
    // 尺寸
    qrCodeSize: {
      type: Number,
      default: 150,
    },
  },
  mounted() {
    if (!this.qrCodeText){
      return
    }
    this.createQrCode()
  },
  methods: {
    createQrCode() {
      this.$nextTick(() => {
        new QRCode(this.$refs.qrCodeUrl, {
          text: this.qrCodeText,
          width: this.qrCodeSize,
          height: this.qrCodeSize,
          colorDark: '#000000',
          colorLight: '#ffffff',
          correctLevel: QRCode.CorrectLevel.H,
        })
      })
    },
  }
}
</script>

<template>
  <div class="qrcode-container" :style="{ width: qrCodeSize + 'px', height: qrCodeSize + 'px'}">
    <div v-if="qrCodeText" ref="qrCodeUrl"></div>
  </div>
</template>

<style scoped>

</style>